/**
 * 福利卡管理服务类
 * 
 */

const request = require('../libs/request');

class CardService {

	/**
	 * 获取福利卡列表（含分页）
	 * @param {Object} params 查询参数
	 * @param {number} params.page 页码，默认1
	 * @param {number} params.pageSize 每页数量，默认10
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回福利卡列表数据
	 */
	static async getCardList(params = {}) {
		const { 
			page = 1, 
			pageSize = 10, 
			showLoading = false 
		} = params;

		try {
			const response = await request.authGet('/v1/card/index', {
				showLoading,
				showErrorInfo: false,
				data: {
					page,
					page_size: pageSize
				}
			});

			// 检查后端返回的状态码
			if (response.code === 1) {
				return {
					success: true,
					data: response.data,
					message: response.msg || '获取福利卡列表成功'
				};
			} else {
				return {
					success: false,
					data: null,
					message: response.msg || '获取福利卡列表失败'
				};
			}
		} catch (error) {
			console.error('获取福利卡列表失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '获取福利卡列表失败'
			};
		}
	}

	/**
	 * 获取福利卡详情
	 * @param {Object} params 查询参数
	 * @param {number} params.id 卡片ID
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回福利卡详情数据
	 */
	static async getCardDetail(params = {}) {
		const { 
			id, 
			showLoading = false 
		} = params;

		if (!id) {
			return {
				success: false,
				data: null,
				message: '卡片ID不能为空'
			};
		}

		try {
			const response = await request.authGet('/v1/card/detail', {
				showLoading,
				data: { id }
			});

			// 检查后端返回的状态码
			if (response.code === 1) {
				return {
					success: true,
					data: response.data,
					message: response.msg || '获取福利卡详情成功'
				};
			} else {
				return {
					success: false,
					data: null,
					message: response.msg || '获取福利卡详情失败'
				};
			}
		} catch (error) {
			console.error('获取福利卡详情失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '获取福利卡详情失败'
			};
		}
	}

	/**
	 * 绑定福利卡
	 * @param {Object} params 绑定参数
	 * @param {string} params.cardNumber 卡密
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回绑定结果
	 */
	static async bindCard(params = {}) {
		const { 
			cardNumber, 
			showLoading = false 
		} = params;

		if (!cardNumber) {
			return {
				success: false,
				data: null,
				message: '卡密不能为空'
			};
		}

		try {
			const response = await request.authPost('/v1/card/bind', {
				showLoading,
				data: {
					card_number: cardNumber
				}
			});

			// 检查后端返回的状态码
			if (response.code === 1) {
				return {
					success: true,
					data: response.data,
					message: response.msg || '绑定成功'
				};
			} else {
				return {
					success: false,
					data: null,
					message: response.msg || '绑定失败'
				};
			}
		} catch (error) {
			console.error('绑定卡片失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '绑定失败'
			};
		}
	}

	/**
	 * 获取当前登录用户所有有效的卡片（未过期且积分大于0）
	 * @param {Object} params 查询参数
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回有效卡片列表数据
	 */
	static async getValidCards(params = {}) {
		const { 
			showLoading = false 
		} = params;

		try {
			const response = await request.authGet('/v1/card/getValidCards', {
				showLoading
			});

			// 检查后端返回的状态码
			if (response.code === 1) {
				return {
					success: true,
					data: response.data,
					message: response.msg || '获取有效卡片列表成功'
				};
			} else {
				return {
					success: false,
					data: null,
					message: response.msg || '获取有效卡片列表失败'
				};
			}
		} catch (error) {
			console.error('获取有效卡片列表失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '获取有效卡片列表失败'
			};
		}
	}

	static async getAllTopics(params = {}) {
		const { 
			showLoading = false 
		} = params;
		try {
			const response = await request.authGet('/v1/card/all-topics', {
				showLoading
			});

			if (response.code === 1) {
				return {
					success: true,
					data: response.data,
					message: response.msg || '获取话题列表成功'
				};
			}else{
				return {
					success: false,
					data: null,
					message: response.msg || '获取话题列表失败'
				};
			}
		} catch (error) {
			console.error('获取话题列表失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '获取话题列表失败'
			};
		}
	}

	static async getTopicList(params = {}) {
		const { 
			page = 1, 
			pageSize = 10, 
			showLoading = false 
		} = params;

		try {
			const response = await request.authGet('/v1/card/topics', {
				showLoading,
				showErrorInfo: false,
				data: { page, page_size: pageSize }
			});

			if (response.code === 1) {
				return {
					success: true,
					data: response.data,
					message: response.msg || '获取话题列表成功'
				};
			} else {
				return {
					success: false,
					data: null,
					message: response.msg || '获取话题列表失败'
				};
			}
		} catch (error) {
			console.error('获取话题列表失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '获取话题列表失败'
			};
		}
	}	

	/**
	 * 购买主题卡
	 * @param {Object} params 购买参数
	 * @param {number} params.cardId 卡片ID
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回购买结果
	 */
	static async buyTopic(params = {}) {
		const { 
			cardId, 
			price,
			showLoading = false 
		} = params;

		if (!cardId) {
			return {
				success: false,
				data: null,
				message: '卡片ID不能为空'
			};
		}

		try {
			const response = await request.authPost('/v1/card/buy-topic', {
				showLoading,
				data: {
					card_id: cardId,
					price: price
				}
			});

			// 检查后端返回的状态码
			if (response.code === 1) {
				return {
					success: true,
					data: response.data,
					message: response.msg || '订单创建成功'
				};
			} else {
				return {
					success: false,
					data: null,
					message: response.msg || '订单创建失败'
				};
			}
		} catch (error) {
			console.error('购买主题卡失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '购买失败'
			};
		}
	}

	/**
	 * 查询主题卡订单状态
	 * @param {Object} params 查询参数
	 * @param {number} params.orderId 订单ID
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回订单状态
	 */
	static async getTopicOrderStatus(params = {}) {
		const { 
			orderId, 
			showLoading = false 
		} = params;

		if (!orderId) {
			return {
				success: false,
				data: null,
				message: '订单ID不能为空'
			};
		}

		try {
			const response = await request.authGet('/v1/card/topic-order-status', {
				showLoading,
				data: {
					order_id: orderId
				}
			});

			// 检查后端返回的状态码
			if (response.code === 1) {
				return {
					success: true,
					data: response.data,
					message: response.msg || '查询成功'
				};
			} else {
				return {
					success: false,
					data: null,
					message: response.msg || '查询失败'
				};
			}
		} catch (error) {
			console.error('查询订单状态失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '查询失败'
			};
		}
	}

	/**
	 * 转赠卡片
	 * @param {Object} params 转赠参数
	 * @param {number} params.cardUserId 卡片用户ID
	 * @param {string} params.targetPhone 目标手机号
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回转赠结果
	 */
	static async transferCard(params = {}) {
		const { 
			cardUserId, 
			targetPhone, 
			showLoading = false 
		} = params;

		if (!cardUserId) {
			return {
				success: false,
				data: null,
				message: '卡片ID不能为空'
			};
		}

		if (!targetPhone) {
			return {
				success: false,
				data: null,
				message: '目标手机号不能为空'
			};
		}

		try {
			const response = await request.authPost('/v1/card/transfer-card', {
				showLoading,
				data: {
					card_user_id: cardUserId,
					target_phone: targetPhone
				}
			});

			// 检查后端返回的状态码
			if (response.code === 1) {
				return {
					success: true,
					data: response.data,
					message: response.msg || '转赠成功'
				};
			} else {
				return {
					success: false,
					data: null,
					message: response.msg || '转赠失败'
				};
			}
		} catch (error) {
			console.error('转赠卡片失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '转赠失败'
			};
		}
	}

	/**
	 * 获取积分日志列表（含分页）
	 * @param {Object} params 查询参数
	 * @param {number} params.page 页码，默认1
	 * @param {number} params.pageSize 每页数量，默认10
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回积分日志列表数据
	 */
	static async getScoreLogs(params = {}) {
		const { 
			page = 1, 
			pageSize = 10, 
			showLoading = false 
		} = params;

		try {
			const response = await request.authGet('/v1/card/logs', {
				showLoading,
				showErrorInfo: false,
				data: {
					page,
					page_size: pageSize
				}
			});

			// 检查后端返回的状态码
			if (response.code === 1) {
				return {
					success: true,
					data: response.data,
					message: response.msg || '获取积分日志成功'
				};
			} else {
				return {
					success: false,
					data: null,
					message: response.msg || '获取积分日志失败'
				};
			}
		} catch (error) {
			console.error('获取积分日志失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '获取积分日志失败'
			};
		}
	}
}

module.exports = CardService; 